package com.spotbook.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.spotbook.R;
import com.spotbook.providers.Contract;

public class SpotListActivity extends FragmentActivity implements
		SpotListFragment.Callbacks {

	private boolean mTwoPane;

	private SpotDetailFragment details;
	private SpotListFragment list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spot_list);
		this.list = (SpotListFragment) getSupportFragmentManager()
				.findFragmentById(R.id.spot_list);
		this.details = (SpotDetailFragment) getSupportFragmentManager()
				.findFragmentById(R.id.spot_details);
		if (this.details != null) {
			mTwoPane = true;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.action_new, menu);
		getMenuInflater().inflate(R.menu.action_map, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_map:
			startActivity(new Intent(this, SpotMapActivity.class));
			return false;
		case R.id.action_new:
			startActivity(new Intent(this, AddSpotActivity.class));
			return false;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onItemSelected(long id) {
		if (mTwoPane) {
			this.details.setData(Contract.build(id));

		} else {
			Intent detailIntent = new Intent(this, SpotDetailActivity.class);
			detailIntent.setData(Contract.build(id));
			startActivity(detailIntent);
		}
	}
}
